import random

from flask import current_app, request
from flask import render_template
from flask.json import jsonify
from sqlalchemy import and_
from sqlalchemy.dialects.postgresql import json

from info import db
from info.models import CategroyMovie, MovieTable, RelaCategroyMovie, tb_categroy_movie, MovieDetailTable, \
    MoviePerformerTable, PerformerDetailTable
from . import index_blu


@index_blu.route('/performer_detail', methods=['GET'])
def performer_detail():
    performer_name = request.args.get('performer_name')
    if performer_name == None:
        return '<h1>请传入演员名字</h1>'
    performer_detail = PerformerDetailTable.query.filter(PerformerDetailTable.name == performer_name).first()
    if performer_detail == None:
        return '<h1>暂未找到%s的资料</h1>' % performer_name
    print(performer_detail)
    return render_template(
        'performer_detail/performer_detail.html',
        performer_detail=performer_detail,
    )


@index_blu.route('/movie_play', methods=['GET'])
def movie_play():
    movie_id = request.args.get('movie_id')
    if movie_id == None:
        return 'error: movie_id is none'

    movie_detail = MovieDetailTable.query.filter(MovieDetailTable.id == movie_id).first()
    if movie_detail == None:
        return '<h1>暂无详情</h1>'
    print(movie_detail)
    movie = MovieTable.query.filter(MovieTable.id == movie_id).first()
    movie_performer = movie.movie_performer.all()
    play_url = 'http://jx.aeidu.cn/index.php?url=%s' % movie.url
    movie_categroy = movie_detail.categroy.split(',')
    des = movie_detail.des.split(',')

    return render_template(
        'movie_play/movie_play.html',
        movie_detail=movie_detail,
        movie_categroy=movie_categroy,
        des=des,
        movie=movie,
        movie_performer=movie_performer,
        play_url=play_url
    )


@index_blu.route('/movie_list', methods=['GET'])
def movie_list():
    classify_id = request.args.get('classify_id')
    page = request.args.get('page')
    if page == None:
        page = 1
    categroy_movies = CategroyMovie.query.all()
    categroys = CategroyMovie.query.with_entities(CategroyMovie.categroy).distinct().all()
    categroy_list = []
    for c in categroys:
        categroy_list.append(
            {
                'categroy': c[0],
                'datas': []
            }
        )
    for c in categroy_movies:
        for ca in categroy_list:
            if ca['categroy'] == c.categroy:
                ca['datas'].append(c)

    if classify_id == None:
        m = MovieTable.query.filter().paginate(int(page), 30, False)
    else:
        c = CategroyMovie.query.filter(CategroyMovie.numid == classify_id).first()
        m = c.movies.paginate(1, 30, False)
        classify_id = int(classify_id)

    movie_list = []
    for i in m.items:
        movie_list.append(i.to_json())
    data = {
        "total_page": range(0, m.pages),
        "current_page": m.page,
        "pages": m.pages,
        "movies": movie_list,
        "select": classify_id
    }

    return render_template('movie_list/movie_list.html', items=categroy_list, movies=data)


@index_blu.route('/')
def movies():
    m = MovieTable.query.filter().paginate(2, 10, False)
    # print(m.pages)
    # print(m.page)
    movie_list = []
    for i in m.items:
        movie_list.append(i.to_json())

    data = {
        "total_page": m.pages,
        "current_page": m.page,
        "movies": movie_list
    }
    return jsonify(errno=200, errmsg="OK", data=data)


@index_blu.route('/index')
def index():
    return render_template('index.html')


@index_blu.route('/favicon.ico')
def favicon():
    return current_app.send_static_file('favicon.ico')
